<!--待审批账单自定义操作-->
<template>
  <el-dropdown @command="handleCommand">
    <span class="el-dropdown-link">
      操作<i class="el-icon-arrow-down el-icon--right"></i>
    </span>
    <el-dropdown-menu slot="dropdown">
      <el-dropdown-item v-if="data.forceLocked!==1&&permission('finance_voucher_edit')" :command="edit">编辑</el-dropdown-item>
      <el-dropdown-item v-if="permission('finance_voucher_view')" :command="review">查看</el-dropdown-item>
      <el-dropdown-item v-if="data.forceLocked!==1&&permission('finance_voucher_force_locked')" :command="lock">锁定</el-dropdown-item>
      <el-dropdown-item v-if="data.forceLocked===1&&permission('finance_voucher_force_locked')" :command="unloc">解锁</el-dropdown-item>
      <el-dropdown-item v-if="permission('finance_voucher_copy')" :command="copy">复制</el-dropdown-item>
      <el-dropdown-item v-if="permission('deleteFinanceVoucherById')" :command="deleted">删除</el-dropdown-item>
    </el-dropdown-menu>
  </el-dropdown>
</template>

<script>
  import { ajax, actionPermission1 } from "@/store/yg/common/utils";
  import { m } from "@/store/yg/common/local";

  export default {
    props: ["row", "data", "more", "editRules"],
    data() {
      return {
        review: "",
        edit: "",
        lock: "",
        unloc: "",
        copy: "",
        deleted: "",
        rules: {},
      };
    },
    created() {},
    mounted() {
      this.review = "review_" + this.data.id;
      this.edit = "edit_" + this.data.id;
      this.lock = "lock_" + this.data.id;
      this.unloc = "unloc_" + this.data.id;
      this.copy = "copy_" + this.data.id;
      this.deleted = "deleted_" + this.data.id;
    },
    methods: {
      /*displayRules() {
        if (this.editRules) {
          this.rules = this.editRules;
        } else {
          for (var i in this.$store.state.main.entry.table.action) {
            let a = this.$store.state.main.entry.table.action[i];
            if (a.displayRule) {
              this.rules[a.client.commit] = a.displayRule;
            }
          }
        }
        console.log(this.rules);
      },*/
      permission(commit) {
          var boo = actionPermission1(this.rules, commit, this.data, this.$store.state);
          return boo;
      },
      handleCommand(command) {
        // 当前操作ID
        const id = command.split("_")[1];
        // 待审批账单
        if (command.indexOf("edit_") > -1) {
          // 新页面打开
          window.open("/#/blank/finance__voucher__main?pid=" + id);
        } else if (command.indexOf("review_") > -1) {
          // 新页面打开
          window.open("/#/blank/finance__voucher__view?pid=" + id);
        }  else if (command.indexOf("copy_") > -1) {
          // 新页面打开
          window.open("/#/blank/finance__voucher__copy?pid=" + id);
        } else if (command.indexOf("lock_") > -1) {
          this.updateForceLocked(1,id);
        } else if (command.indexOf("unloc_") > -1) {
          this.updateForceLocked(0,id);
        } else if (command.indexOf("deleted_") > -1) {
            this.$confirm("确定要删除么?", "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
            })
            .then(() => {
              // 标记为已销账
              ajax("deleteFinanceVoucherByIdView", {id:id}).then(
                res => {
                  const code = res.code;
                  if (code === 200) {
                    this.$message.success("删除成功");
                  } else {
                    this.$message.error(m(res.message));
                  }
                },
                error => {
                  console.log(error);
                }
              );
            })
            .catch(() => {});
        }
      },
      updateForceLocked(status,id){
        this.$confirm("确定执行此操作么?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            // 标记为已销账
            ajax("updateForceLocked", {"id":id, "forceLocked":status}).then(
              res => {
                const code = res.code;
                if (code === 200) {
                  this.$message.success("操作成功");
                  setTimeout(() => {
                    window.location.reload(true);
                  }, 500);
                } else {
                  this.$message.error(m(res.message));
                }
              },
              error => {
                console.log(error);
              }
            );
          })
          .catch(() => {});
      }
    }
  };
</script>
